﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.CO;
using HiStaff.Domain.Sys;
using HiStaff.iSystem;

namespace HiStaff.iTime
{
    public partial class Formular : FormBase
    {
        public decimal? FMLID { get; set; }

        public Formular()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            var lstSource = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>(HiStaff.iSystem.Data.DATALIST.Tables["COSIGNGROUP"]);
            grdFmlDetail.RootTable.Columns["GROUPID"].ValueList.PopulateValueList(lstSource, "ID", "NAME");
            grdSign.RootTable.Columns["GROUPID"].ValueList.PopulateValueList(lstSource, "ID", "NAME");
            base.CreateCommandBar(CommandBarManager.ActionCommand.Edit,
                    CommandBarManager.ActionCommand.Cancel,
                    CommandBarManager.ActionCommand.Save,
                    CommandBarManager.ActionCommand.Separator,
                    CommandBarManager.ActionCommand.Validate,
                    CommandBarManager.ActionCommand.Separator,
                    CommandBarManager.ActionCommand.Exit);
            grdSign.DataSource = HiStaff.iSystem.Data.ALLCOSIGN;
            BindData();
            return base.FormLoad();
        }
        private void BindData()
        {
            grdFmlDetail.DataSource = Service.TimeClient.GetFormularDetail(FMLID);
            grdFmlDetail.Refresh();
        }
        protected override bool SaveButtonClick()
        {
            CO_FML_DETAIL objSave = new CO_FML_DETAIL();
            ValueControlsToObject(objSave, this);
            objSave.FMLID = FMLID;
            if (Service.ExcuteStoreNonQuery("APRU_CO_FML_DETAIL", objSave))
            {
                BindData();
                return true;
            }
            return false;
        }
        protected override bool CancelButtonClick()
        {
            BindDataFromGridToForm(grdFmlDetail, this);
            return true;
        }
        protected override bool ValidateButtonClick()
        {
            if (!string.IsNullOrEmpty(hiLabel2.Text) && !string.IsNullOrEmpty(txtFmlDetail.Text))
            {
                var obj = new CO_FML_DETAIL { FORMULAR = txtFmlDetail.Text, CODE = hiLabel2.Text };
                Service.ExcuteStoreNonQuery("PRO_CHK_COFORMULAR", obj);
                if (string.IsNullOrEmpty(obj.ERRCODE))
                {
                    MessageBox.Show(GetMessageById("MSG_CHECK_FORMULAR_SUCCESSFUL"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
                else
                {
                    MessageBox.Show(GetMessageById("MSG_CHECK_FORMULAR_UNSUCCESSFUL") + "\n" + obj.ERRMSG, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
            return false;
        }
        private void grdFmlDetail_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGridToForm(grdFmlDetail, this);
        }
    }
}
